#include <string.h>
#include "ota_ble.h"
#include "ota_hal_trans.h"
#include "ota_import.h"

int ota_ble_transport_auth(unsigned char is_auth)
{
    int ret = 0;
    ota_ble_global_dat_t *p_ota = ota_ble_get_global_data_center();
    if(p_ota != NULL) {
        if (ota_ble_check_upgrade_result() == 0) {
            unsigned char fwup_success = 0x01;
            if(ota_ble_transport(OTA_BLE_CMD_FW_UPDATE_PROCESS, &fwup_success, 1) == OTA_SUCCESS) {
                OTA_LOG_I("OTA OK!");
            } else {
                OTA_LOG_E("send err!");
            }
        }
        if(ota_hal_rollback() != 0) {
            OTA_LOG_E("ble ota clear flag faild!");
        }
        if (is_auth != 0) {
            if (p_ota->status == OTA_BLE_STATE_OFF) {
                p_ota->status = OTA_BLE_STATE_IDLE;
            } else {
                OTA_LOG_E("err status");
            }
        }
    }
    OTA_LOG_I("auth over");
    return ret;
}

int ota_ble_send_bytes_received()
{
    int err_code = -1;
    unsigned char tx_buff[16];
    ota_ble_global_dat_t *p_ota = ota_ble_get_global_data_center();
    if(p_ota != NULL) {
        memset(tx_buff, 0x00, sizeof(tx_buff));
        ENCODE_U16(tx_buff, p_ota->frames_recvd);
        ENCODE_U32(tx_buff + sizeof(unsigned short), p_ota->bytes_recvd);
        err_code = breeze_post_ext(OTA_BLE_CMD_FW_BYTES_RECEIVED, tx_buff, sizeof(unsigned short) + sizeof(unsigned int));
        if (err_code != OTA_SUCCESS) {
            OTA_LOG_E("send rec err");
        }
    }
    return err_code;
}

int ota_ble_transport_done(unsigned char cmd)
{
    int ret = 0;
    ota_ble_global_dat_t *p_ota = ota_ble_get_global_data_center();
    if(p_ota != NULL) {
        if(p_ota->status == OTA_BLE_STATE_RESET_PREPARE) {
            if (cmd == OTA_BLE_CMD_FW_CHECK_RESULT) {
                ota_msleep(2000);
                ota_ble_disconnect();
            }
        }
    }
    return ret;
}

int ota_ble_transport_err_frame(void)
{
    int ret = OTA_INIT_FAIL;
    unsigned char tx_buff[16];
    ota_ble_global_dat_t *p_ota = ota_ble_get_global_data_center();
    if(p_ota != NULL) {
        memset(tx_buff, 0x00, sizeof(tx_buff));
        ENCODE_U16(tx_buff, p_ota->frames_recvd);
        ENCODE_U32(tx_buff + sizeof(unsigned short), p_ota->bytes_recvd);
        ret = ota_ble_transport(OTA_BLE_CMD_FW_BYTES_RECEIVED, tx_buff, sizeof(unsigned short) + sizeof(unsigned int));
        if (ret != OTA_SUCCESS) {
            OTA_LOG_E("send rec err");
        }
    }
    return ret;
}

void ota_ble_transport_msg(breeze_otainfo_t *ble_info)
{
    int ret = 0;
    if(ble_info != NULL) {
        if(ble_info->type == OTA_CMD) {
            ret = ota_ble_download(ble_info->cmd_evt.m_cmd.cmd, ble_info->cmd_evt.m_cmd.frame, ble_info->cmd_evt.m_cmd.data, ble_info->cmd_evt.m_cmd.len);
        } else if(ble_info->type == OTA_EVT) {
            switch(ble_info->cmd_evt.m_evt.evt) {
                case ALI_OTA_ON_AUTH_EVT:
                   ota_ble_transport_auth(ble_info->cmd_evt.m_evt.d);
                   break;
                case ALI_OTA_ON_TX_DONE:
                   ota_ble_transport_done(ble_info->cmd_evt.m_evt.d);
                   break;
                case ALI_OTA_ON_DISCONNECTED:
                   ota_msleep(1000);
                   ota_reboot();
                   break;
                case ALI_OTA_ON_DISCONTINUE_ERR:
                   ret = ota_ble_transport_err_frame();
                   if(ret != OTA_SUCCESS) {
                       OTA_LOG_E("err-frame resent fail");
                   }
                   break;
                default:
                   break;
            }
        }
    }
}
